package cn.edu.sig.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import cn.edu.sig.bean.CurrentPage;
import cn.edu.sig.bean.Style;
import cn.edu.sig.dao.StyleDao;
import cn.edu.sig.util.PaginationHelper;
@Repository("styleDao")
public class StyleDaoImpl extends TemplateSupport implements StyleDao {

	public void add(Style style) {
		
		String sql = this.sqlSource.getSql("style.add");

	}

	public void delete(Integer id) {
		// TODO Auto-generated method stub

	}

	public void edit(Style style) {
		// TODO Auto-generated method stub

	}

	public List<Style> findAll() {
		String sql = this.sqlSource.getSql("style.list");

		RowMapper<Style> mapper = new RowMapper<Style>() {
			public Style mapRow(ResultSet rs, int rowNum) throws SQLException {

				Style style = new Style(); 
				
				style.setStyleId(rs.getInt("styleId"));
				style.setStyleName(rs.getString("styleName"));
				if(rs.getInt("styleId") != 0){
					style.setStyle(findParent(rs.getInt("parentId")));
				}
				return style;
			}
		};

		return this.getJdbcTemplate().query(sql, mapper);
	}

	public Style findById(String id) {
		
		String sql = this.sqlSource.getSql("style.findById");
		
		RowMapper<Style> mapper = new RowMapper<Style>() {
			public Style mapRow(ResultSet rs, int rowNum) throws SQLException {

				Style style = new Style();
				style.setStyleId(rs.getInt("styleId"));
				style.setStyleName(rs.getString("styleName"));
				if(rs.getInt("styleId") != 0){
					style.setStyle(findParent(rs.getInt("parentId")));
				}
				return style;
			}
		};

		return (Style)this.getJdbcTemplate().queryForObject(sql, new Object[] { id },
				mapper);
	}

	public Style findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	public Style findParent(int id) {
		String sql = this.sqlSource.getSql("style.findById");
		
		RowMapper<Style> userMapper = new RowMapper<Style>() {
			public Style mapRow(ResultSet rs, int rowNum) throws SQLException {

				Style style = new Style();
				if(rs.getInt("styleId") >= 0)
				style.setStyleId(rs.getInt("styleId"));
				style.setStyleName(rs.getString("styleName"));
				return style;
			}
		};

		return (Style)this.getJdbcTemplate().queryForObject(sql, new Object[] { id },
				userMapper);
	}

	public CurrentPage<Style> findChild(int id, int pageNo, int pageSize) {
		PaginationHelper<Style> ph = new PaginationHelper<Style>();
        return ph.fetchPage(
        		this.getJdbcTemplate(),
                "SELECT count(*) FROM style WHERE parentId = ?",
                "SELECT * FROM style WHERE parentId = ? Limit ? ,?",
                String.valueOf(id),
                pageNo,
                pageSize,
                new ParameterizedRowMapper<Style>() {
                    public Style mapRow(ResultSet rs, int i) throws SQLException {
                        
                    	Style style = new Style();
                    	style.setStyleId(rs.getInt("styleId"));
                    	style.setStyleName(rs.getString("styleName"));
                    	if(rs.getInt("styleId") != 0){
        					style.setStyle(findParent(rs.getInt("parentId")));
        				}
                    	
                    	return style;
                    }
                }
        );

	}



	public List<Style> findStyle(int id) {
		String sql = this.sqlSource.getSql("style.findChilds");

		RowMapper<Style> mapper = new RowMapper<Style>() {
			public Style mapRow(ResultSet rs, int rowNum) throws SQLException {

				Style style = new Style();
				style.setStyleId(rs.getInt("styleId"));
				style.setStyleName(rs.getString("styleName"));
				if(rs.getInt("styleId") != 0){
					style.setStyle(findParent(rs.getInt("parentId")));
				}
				
				return style;
			}
		};
		
		return this.getJdbcTemplate().query(sql, new Object[] {id}, mapper);
	}

}
